package com.hnevc.mytrafficdemo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hnevc.mytrafficdemo.R;

import java.util.List;

/**
 * Created by Administrator on 2017/12/18.
 */

public class SlidingMenuAdapter extends BaseAdapter {
    //声明用于存放"字母"图标的集合
    List<Integer> icons;
    //声明用于存放"内容"的集合
    List<String> contents;
    //声明上下文对象
    Context context;
    //声明 LayoutInflater
    LayoutInflater inflater;

    /**
     * 无参数的构造方法
     */
    public SlidingMenuAdapter(){

    }
    /**
     * 含有三个参数的构造方法
     */
    public SlidingMenuAdapter( Context context,List<String> contents,List<Integer> icons ){
        this.context = context;
        this.contents = contents;
        this.icons = icons;
        inflater = LayoutInflater.from(context);
    }
    /**
     * How many items are in the data set represented by this Adapter.
     * 该方法表示: ListView 中要放多少条条目数据
     * @return Count of items,返回数据集总个数即可.数据集是数组,返回.length; 集合返回集合长度
     */
    @Override
    public int getCount() {
        //由于图标集合的数据集个数 与 存放内容的数据集个数相同,所以以下返回哪个都可以
        //return contents.size();
        return icons.size();
    }

    /**
     * Get the data item associated with the specified position in the data set.
     * 该方法用于获取position位置上的那个item上的数据
     * @param position Position of the item whose data we want within the adapter's
     *                 data set.
     * @return The data at the specified position.
     */
    @Override
    public Object getItem(int position) {
        //返回 return contents.get(position);
        return icons.get(position);
    }

    /**
     * Get the row id associated with the specified position in the list.
     * 该方法用于获取 position 位置上的那个item在的id
     * @param position The position of the item within the adapter's data set whose row id we want.
     * @return The id of the item at the specified position.
     */
    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * Get a View that displays the data at the specified position in the data set. You can either
     * create a View manually or inflate it from an XML layout file. When the View is inflated, the
     * parent View (GridView, ListView...) will apply default layout parameters unless you use
     * {@link LayoutInflater#inflate(int, ViewGroup, boolean)}
     * to specify a root view and to prevent attachment to the root.
     *
     * @param position    The position of the item within the adapter's data set of the item whose view
     *                    we want.
     * @param convertView The old view to reuse, if possible. Note: You should check that this view
     *                    is non-null and of an appropriate type before using. If it is not possible to convert
     *                    this view to display the correct data, this method can create a new view.
     *                    Heterogeneous lists can specify their number of view types, so that this View is
     *                    always of the right type (see {@link #getViewTypeCount()} and
     *                    {@link #getItemViewType(int)}).
     * @param parent      The parent that this view will eventually be attached to
     * @return A View corresponding to the data at the specified position.
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //声明listView中的item布局文件中的控件
        ImageView iconIv;
        TextView contentTv;
        //step1.判断 convertView 是否为空,为空创建
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.slidingmenu_listview_item,parent,false);
        }
        //step2.查找item布局文件中控件
        iconIv = (ImageView)convertView.findViewById(R.id.slidingmenu_lv_letter_iv);
        contentTv = (TextView) convertView.findViewById(R.id.slidingmenu_lv_content_tv);
        //step3.为item中的控件设置数据
        iconIv.setImageResource(icons.get(position));
        contentTv.setText(contents.get(position));



        //step4.返回convertView
        return convertView;
    }
}
